AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include('shared.lua')     

function ENT:Initialize()
	self:DrawShadow( true )
	self:SetModel( "models/props/de_inferno/crate_fruit_break_gib2.mdl" )
	self:PhysicsInit( SOLID_VPHYSICS )
	self:SetMoveType( MOVETYPE_VPHYSICS )
	self:SetSolid( SOLID_VPHYSICS )

	local phys = self:GetPhysicsObject()  	
	if (phys:IsValid()) then  		
		phys:Wake()
		phys:EnableMotion(true)
	end
	self:GetPhysicsObject():SetMaterial( "metal" )
	self:SetMaterial( "models/shiny" ) --oooh, shiny
	self:SetColor( 0,0,0,255 ) --i call this "rip in space-time black"
end

function ENT:SpawnFunction( ply, tr )
	if not tr.Hit then return end
	
	local ent = ents.Create( self.Classname )
	ent:SetPos( tr.HitPos + tr.HitNormal * 8 )
	ent:Spawn()
	ent:GetPhysicsObject():SetMaterial( "metal" )
	ent:Activate()
	
	return ent
end

function ENT:PhysicsCollide(tblData)
	if self:GetNetworkedBool( "fired" ) then --if the cannonball has been fired from a cannon...
		if tblData.Speed > 20 then --if it's going kinda fast
			self.shouldexplode = self:GetNetworkedBool("explode")
			self.shoulddestroy = self:GetNetworkedBool("destroy")
			self.shouldignite = self:GetNetworkedBool("ignite")
			self:Destruct( true, self.shouldexplode, self.shoulddestroy, self.shouldignite ) --cause some havoc (and explode/kill itself afterwards)
		end
	end
end

function ENT:Destruct( delself, explode, destroy, ignite ) --this is where things get AWESOME (delself kills the ball, explode makes it explode)
	for k,v in pairs( self:GetNearProps(75) ) do --look at the props around the ball
		if v:IsValid() then
			local class = v:GetClass()
			if destroy then
				if v:IsNPC() or class == "prop_physics" or class == "sd_res" or class == "gmod_wheel" or class == "phys_magnet" then --if it's a general prop or npc...
					timer.Simple( 15, function() if v:IsValid() then FadeOut( v ) end end )
					v:GetPhysicsObject():EnableMotion( true ) --unfreeze it!
					if constraint.HasConstraints( v ) then --if it has any constraints...
						constraint.RemoveAll( v ) --undo them!
					end
					if ignite then 
						v:Ignite( 10, 10 ) --ignite it!
						timer.Simple( 9, function() if v:IsValid() then v:SetMaterial( "models/props_debris/plasterwall009d.vmt" ) end end )
					end
				elseif v:IsPlayer() then --if it's a player...
					if explode then --if we want splash damage...
						v:Kill() --kill it!
					end
				end
			end
		end
	end
	
	if delself then
		self:Remove() --it was fun, but all good things must come to an end
	end
	
	if explode then
		self:EmitExplodeEffects()
	end
end

function ENT:GetNearProps( radius )
	local tblaround = ents.FindInSphere( self:GetPos(), radius )
	return tblaround
end

function ENT:EmitExplodeEffects() --looks cool
	local explode = EffectData()
	explode:SetStart( self:GetPos() )
	explode:SetOrigin( self:GetPos() )
	explode:SetScale( 0.1 )
	util.Effect( "Explosion", explode, true, true )
end

function ENT:KickBall( fmul ) --forward multiplier
	--self:GetPhysicsObject():ApplyForceCenter( (self:GetForward() * fmul) * 250000 )
	--self:GetPhysicsObject():ApplyForceCenter(Vector( 0, math.random(-10000 , 10000 ), 0 ))
	self:GetPhysicsObject():SetVelocity( (self:GetForward() * 800) * fmul)
end